<template>
	<view class="add bg_color pdd_bottom2">
		<view v-if="!isEnter">
			<view class="tit">
				<view class="text">举报信息</view>
				<view class="xing">*</view>
			</view>
			<Form ref="formRef" :formList="formList" :formData="formData"></Form>
			<view class="tit">
				<view class="text">举报人信息</view>
				<view class="xing">*</view>
			</view>
			<view class="dangshiren">
				<Form ref="formRefs" :formList="dangShi" :formData="formData"></Form>
			</view>
			<view class="butt font28" @click="addItem">提交</view>
		</view>
		<SubmitState v-else :success="success" @successReturn="fanhui" @failReturn="isEnter = false"></SubmitState>
	</view>
	<FuJinSubmitModal :deleteShow="deleteShow1" @confirm="confirm1" @close="deleteShow1 = false"></FuJinSubmitModal>
</template>
<script>
import changeData from 'utils/changeData.js'
export const formList = [
	{
		label: '标题',
		prop: 'complaintsTitle',
		type: 'text',
		placeholder: '请输入标题',
		rules: [
			{
				required: true,
				message: '请输入标题',
				trigger: ['change', 'blur']
			}
		]
	},
	
	{
		label: '时间',
		prop: 'complaintsTime',
		type: 'datetime',
		placeholder: '请选择时间',
		mode: 'datetime',
		rules: [
			{
				required: true,
				message: '请选择时间',
				trigger: ['change', 'blur']
			}
		],
		format: 'YYYY-MM-DD HH:mm'
	},
	{
		label: '所在地区',
		prop: 'province',
		type: 'picker',
		placeholder: '请选择所在地区',
		rules: [
			{
				required: true,
				message: '请选择所在地区',
				trigger: ['change', 'blur']
			}
		]
	},
	{
		label: '详细地址',
		prop: 'complaintsPlace',
		type: 'textarea',
		rules: [
			{
				required: true,
				message: '请输入地址',
				trigger: ['change', 'blur']
			}
		],
		placeholder: '请输入地址'
	},
	{
		label: '具体情况',
		prop: 'complaintsContent',
		type: 'textarea',
		rules: [
			{
				required: true,
				message: '请输入内容',
				trigger: ['change', 'blur']
			}
		],
		placeholder: '请输入内容'
	},
	{
		label: '附件（可选）',
		prop: 'complaintsFile',
		type: 'upload',
		placeholder: '请输入内容'
	}
];
export const dangShi = [
	{
		label: '姓名',
		prop: 'complaintsName',
		type: 'text',
		rules: [
			{
				required: true,
				message: '请输入姓名',
				trigger: ['change', 'blur']
			}
		]
	},
	{
		label: '电话号码',
		prop: 'complaintsPhone',
		type: 'text',
		rules: [
			{
				required: true,
				message: '请输入正确的电话号码',
				pattern: changeData.phonePattern,
				trigger: ['change', 'blur']
			}
		]
	},
	{
		label: '身份证号码',
		prop: 'complaintsCard',
		type: 'text',
		rules: [
			{
				required: true,
				message: '请输入正确的身份证号码',
				validator: changeData.cardRouls,
				trigger: ['change', 'blur']
			}
		]
	}
];
</script>
<script setup>
import Form from 'com/Form.vue';
import SubmitState from 'com/modal/SubmitState.vue';
import FuJinSubmitModal from 'com/modal/FuJinSubmitModal.vue';
import Api from 'api';
import { usersStore } from 'store/users.js';
const api = new Api
const store = usersStore();
const id = ref('');
const formRef = ref(null);
const formRefs = ref(null);
const isEnter = ref(false);
const deleteShow1 = ref(false);
const success = ref(false);
onLoad(async (query) => {
	if (query.id) {
		uni.setNavigationBarTitle({
					title: '编辑投诉举报'
				})
		id.value = query.id;
		const res = await api.queryById('complaintsReport',{ id: query.id });
		formData.value = res;
	}else {
		uni.setNavigationBarTitle({
			title: '新增投诉举报'
		})
	}
});
const formData = ref({
	complaintsTitle: '',
	complaintsTime: '',
	province: '',
	complaintsPlace: '',
	complaintsContent: '',
	complaintsPhone: store.users.phone,
	complaintsName: store.users.username,
	complaintsCard: '',
	complaintsFile:[]
});
const fanhui = () => {
	uni.navigateBack()
};
const addItem = () => {
	const mise = new Promise((resolve) => {
			formRef.value.submit(() => {
				resolve();
			});
		});
		const mise1 = new Promise((resolve) => {
			formRefs.value.submit(() => {
				resolve();
			});
		});
		let submitPromises = [mise, mise1];
		Promise.all(submitPromises).then(() => {
			store.isFuJing() ? (deleteShow1.value = true) : end();
		});
	}
const confirm1 = ()=>{
	end()
}
const end=async()=>{
	deleteShow1.value = false
	let res;
	if (id.value) {
		res = await api.edit('complaintsReport',unref(formData));
	} else {
		res = await api.add('complaintsReport',unref(formData));
	}
	success.value = res;
	isEnter.value = true;
}
</script>

<style lang="scss" scoped></style>
